package io.renren.modules.generator.service.impl;

import io.renren.common.exception.RRException;
import io.renren.common.validator.Assert;
import io.renren.modules.app.form.LoginForm;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.generator.dao.TMemberDao;
import io.renren.modules.generator.entity.TMemberEntity;
import io.renren.modules.generator.service.TMemberService;


@Service("tMemberService")
public class TMemberServiceImpl extends ServiceImpl<TMemberDao, TMemberEntity> implements TMemberService {

    @Autowired
    private TMemberService memberService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TMemberEntity> page = this.page(
                new Query<TMemberEntity>().getPage(params),
                new QueryWrapper<TMemberEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    public long login(LoginForm form) {

        TMemberEntity memberEntity=memberService.getOne(new QueryWrapper<TMemberEntity>().eq("email",form.getEmail()));
        Assert.isNull(memberEntity, "email or password is wrong");
        if(!memberEntity.getPassword().equals(DigestUtils.sha256Hex(form.getPassword()))){
            throw new RRException("email or password is wrong");
        }
        return memberEntity.getUserid();
    }


    @Override
    public Integer getMostLevel(Integer userId) {
        return baseMapper.getMostLevel(userId);
    }
}